<template>
	<view class="tab-bar">
		<scroll-view id="tab-bar" class="swiper-tab" scroll-x :scroll-left="scrollLeft">
			<view v-for="(tab,index) in tabBars" :key="tab.id" :class="['swiper-tab-list',tabIndex == index ? 'active' : '']" :id="tab.id"
			 :data-current="index" @click="tapTab(index)">{{tab.name}}</view>
		</scroll-view>
		<swiper :current="tabIndex" class="swiper-box" duration="300" @change="changeTab">
			<swiper-item class="ranking_list_1">
				<scroll-view class="goods_list" scroll-y>
					<goods-list v-for="(item, index) in goodsList[0].list" :key="index" v-bind:item="item" v-bind:ranking="index+1"></goods-list>
				</scroll-view>
			</swiper-item>
			<swiper-item class="ranking_list_2">
				<scroll-view class="goods_list" scroll-y>
					<goods-list v-for="(item, index) in goodsList[0].list" :key="index" v-bind:item="item" v-bind:ranking="index+1"></goods-list>
				</scroll-view>
			</swiper-item>
			<swiper-item class="ranking_list_3">
				<scroll-view class="scrollbox" scroll-y>
					<brand-list v-for="(item,i) in brand" :key="i" v-bind:item="item" v-bind:ranking="i+1"></brand-list>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>
<script>
	import goodsList from '@/components/goods-list';
	import brandList from '@/components/brand-list';
	export default {
		components: {
			goodsList,
			brandList
		},
		data() {
			return {
				loadingText: {
					contentdown: "上拉显示更多",
					contentrefresh: "正在加载...",
					contentnomore: "没有更多数据了"
				},
				scrollLeft: 0,
				isClickChange: false,
				tabIndex: 0,
				goodsList: [{}, {}],
				"brand": [{
					"name" : "安瑞井ORIGIN",
					"id": "1",
					"area":"广东省 深圳市",
					"logo":"/static/baner_logo_2.jpg",
					"country" : "/static/country/FR.png",
					"count" : "190",
					"sale" : "10",
					"review" : "98.88"
				},
				{
					"name" : "金利来Goldlion",
					"id": "1",
					"area":"广东省 深圳市",
					"logo":"/static/baner_logo_3.jpg",
					"country" : "/static/country/FR.png",
					"count" : "100",
					"sale" : "0",
					"review" : "98.88"
				},
				{
					"name" : "登喜路Alfred Dunhill",
					"id": "1",
					"area":"广东省 深圳市",
					"logo":"/static/baner_logo_4.jpg",
					"country" : "/static/country/FR.png",
					"count" : "13",
					"sale" : "0",
					"review" : "98.88"
				},
				{
					"name" : "安瑞井ORIGIN",
					"id": "1",
					"area":"广东省 深圳市",
					"logo":"/static/baner_logo_5.jpg",
					"country" : "/static/country/FR.png",
					"count" : "190",
					"sale" : "10",
					"review" : "98.88"
				},
				{
					"name" : "金利来Goldlion",
					"id": "1",
					"area":"广东省 深圳市",
					"logo":"/static/baner_logo_3.jpg",
					"country" : "/static/country/FR.png",
					"count" : "100",
					"sale" : "0",
					"review" : "98.88"
				},
				{
					"name" : "登喜路Alfred Dunhill",
					"id": "1",
					"area":"广东省 深圳市",
					"logo":"/static/baner_logo_3.jpg",
					"country" : "/static/country/FR.png",
					"count" : "13",
					"sale" : "0",
					"review" : "98.88"
				},
				{
					"name" : "金利来Goldlion",
					"id": "1",
					"area":"广东省 深圳市",
					"logo":"/static/baner_logo_3.jpg",
					"country" : "/static/country/FR.png",
					"count" : "100",
					"sale" : "0",
					"review" : "98.88"
				},
				{
					"name" : "登喜路Alfred Dunhill",
					"id": "1",
					"area":"广东省 深圳市",
					"logo":"/static/baner_logo_3.jpg",
					"country" : "/static/country/FR.png",
					"count" : "13",
					"sale" : "0",
					"review" : "98.88"
				}],
				tabBars: [{
					name: '热销榜',
					id: 'system'
				}, {
					name: '产品收藏榜',
					id: 'new'
				}, {
					name: '品牌关注榜',
					id: 'transaction'
				}]
			}
		},
		onLoad(){
			this.loadData();
			setTimeout(()=> {
				this.renderImage = true;
			}, 300);
		},
		methods: {
			goDetail(e) {
				uni.navigateTo({
					url: '/pages/ucenter/mail_detail?id=' + e.id
				})
			},
			async changeTab(e) {
				let index = e.detail.current;
				if (this.isClickChange) {
					this.tabIndex = index;
					this.isClickChange = false;
					return;
				}
				let tabBar = await this.getElSize("tab-bar"),
					tabBarScrollLeft = tabBar.scrollLeft;
				let width = 0;

				for (let i = 0; i < index; i++) {
					let result = await this.getElSize(this.tabBars[i].id);
					width += result.width;
				}
				let winWidth = uni.getSystemInfoSync().windowWidth,
					nowElement = await this.getElSize(this.tabBars[index].id),
					nowWidth = nowElement.width;
				if (width + nowWidth - tabBarScrollLeft > winWidth) {
					this.scrollLeft = width + nowWidth - winWidth;
				}
				if (width < tabBarScrollLeft) {
					this.scrollLeft = width;
				}
				this.isClickChange = false;
				this.tabIndex = index; //一旦访问data就会出问题
			},
			getElSize(id) { //得到元素的size
				return new Promise((res, rej) => {
					uni.createSelectorQuery().select('#' + id).fields({
						size: true,
						scrollOffset: true
					}, (data) => {
						res(data);
					}).exec();
				});
			},
			async tapTab(index) { //点击tab-bar
				if (this.tabIndex === index) {
					return false;
				} else {
					let tabBar = await this.getElSize("tab-bar"),
						tabBarScrollLeft = tabBar.scrollLeft; //点击的时候记录并设置scrollLeft
					this.scrollLeft = tabBarScrollLeft;
					this.isClickChange = true;
					this.tabIndex = index;
				}
			},
			async loadData() {
				let goodsList = await this.$api.json('goodsList');
				this.goodsList = goodsList;
			},
		}
	}
</script>

<style>
page { height:calc(100%)!important; }
.ranking_list_1,.ranking_list_2,.ranking_list_3 { width:100%; height:calc(100%)!important; overflow:auto; background:#ffffff; text-align:left; }
</style>